<template>
    <div id="dashboardBarChart" :style="{ width: '100%', height: '280px' }"></div>
</template>

<script>
import * as echarts from 'echarts'

export default {
    props: {
        lineData: {
            type: Array,
            default() {
                return [1, 2, 3, 4, 5, 6, 7]
            }
        },
    },
    data() {
        return {}
    },
    watch: {
        lineData(val) {
            this.lineData = val
            this.drawLine()
        },
    },
    mounted() {
        this.drawLine()
    },
    methods: {
        drawLine() {
            let myChart = new echarts.init(document.getElementById('dashboardBarChart'))
            myChart.setOption({
                title: {
                    text: '',
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow',
                        label: {
                            backgroundColor: '#6a7985',
                        },
                    },
                },
                color: ['#38A0FF', '#4ECB73'],
                legend: {
                    show: true,
                    data: ['消息数1', '消息数2'],
                    left: 'center',
                    bottom: 0,
                    itemWidth: 14,
                    itemHeight: 14,
                    itemGap: 60,
                    textStyle: {
                        color: '#ccc',
                        fontSize: 14,
                        padding: [0, 40, 0, 5],
                    },
                },
                grid: {
                    top: '3%',
                    left: '0%',
                    right: '3%',
                    bottom: '11%',
                    containLabel: true,
                },
                xAxis: {
                    type: 'category',
                    data: ['初一', '初二', '初三', '高一', '高二', '高三', '教师'],
                    axisTick: {
                        show: true,
                    },
                    axisLine: {
                        lineStyle: {
                            color: "#999",
                            type: "dashed"
                        }
                    }
                },
                yAxis: {
                    type: 'value',
                    splitLine: {
                        show: true,
                        lineStyle: {
                            type: "dashed"
                        }
                    },
                    axisLabel: {
                        show: true,
                    },
                },
                series: [
                    {
                        name: '消息数1',
                        type: 'bar',
                        itemStyle: {
                            color: '#7DAAC8',
                            normal: {
                                color: function (params) {
                                    return new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                        {
                                            offset: 0,
                                            color: '#38A0FF',
                                        },
                                        {
                                            offset: 1,
                                            color: '#38A0FF80',
                                        },
                                    ])
                                },
                            },
                        },
                        data: this.lineData,
                    },
                    {
                        name: '消息数2',
                        type: 'bar',
                        itemStyle: {
                            color: '#7DAAC8',
                            normal: {
                                color: function (params) {
                                    return new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                        {
                                            offset: 0,
                                            color: '#4ECB73',
                                        },
                                        {
                                            offset: 1,
                                            color: '#4ECB7380',
                                        },
                                    ])
                                },
                            },
                        },
                        lineStyle: {
                            width: 2,
                        },
                        areaStyle: {
                            normal: {
                                //颜色渐变函数 前四个参数分别表示四个位置依次为左、下、右、上
                                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                    {
                                        offset: 0,
                                        color: 'rgba(80,141,255,0.8)',
                                    },
                                    {
                                        offset: 0.34,
                                        color: 'rgba(56,155,255,0.4)',
                                    },
                                    {
                                        offset: 1,
                                        color: 'rgba(38,197,254,0.1)',
                                    },
                                ]),
                            },
                        },
                        symbolSize: 0,
                        smooth: true,
                        data: this.lineData,
                    },
                ],
            })
        },
    },
}
</script>
